package com.plwf.lake.service.impl;

import com.plwf.lake.entity.Period;
import com.plwf.lake.mapper.PeriodMapper;
import com.plwf.lake.mapper.ReverseMapper;
import com.plwf.lake.service.PeriodService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PeriodServiceImpl implements PeriodService{
    @Resource
    private PeriodMapper mapper;
    @Resource
    private ReverseMapper dao;
    @Override
    public Period query(Period bean) {
        return this.mapper.select(bean);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED,rollbackFor=SQLException.class)
    public Boolean save(Map<String,Object> map) {
        Boolean flag = false;
        Map<String,String> map2 = new HashMap<>();
        Integer id = (Integer) map.get("id");
        Integer stuId = (Integer) map.get("stuId");
        Integer subId = (Integer) map.get("subId");
        Integer hour = (Integer) map.get("hour");
        Period bean = new Period();
        bean.setStuId(stuId);
        bean.setSubId(subId);
        bean.setHour(hour);
        Period item = mapper.select(bean);
        //bean.setId(pid);
        if(dao.updstatus(id)){
            if(item != null){
                bean.setId(item.getId());
                if(this.mapper.update(bean)){
                    flag = true;
                }
            }else{
                if(this.mapper.insert(bean)){
                    flag = true;
                }
            }
        }
        return flag;
    }
}
